package org.pan.domain.db;

import org.pan.domain.DomainObject;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户组
 *
 * Created by panmingzhi on 2015/12/24.
 */
@Entity
public class CardUserGroup extends DomainObject implements ITreeNode<CardUserGroup> {

    public enum Property{
        code,name,users,parent,children
    }

    // 用户组编码
    @Column(name = "code", nullable = false)
    private String code;

    // 用户组名称
    @Column(name = "name", nullable = false)
    private String name;

    // 用户组所保函的用户
    @ManyToMany(mappedBy = "groups")
    private List<CardUser> users = new ArrayList<>();

    // 此用户组的父用户组
    @ManyToOne
    @JoinColumn(name = "parentGroup", nullable = true, insertable = false, updatable = false)
    private CardUserGroup parent;

    // 此用户组下属的子用户组
    @OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE)
    private List<CardUserGroup> children = new ArrayList<>();

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<CardUser> getUsers() {
        return users;
    }

    public void setUsers(List<CardUser> users) {
        this.users = users;
    }

    @Override
    public void setParent(CardUserGroup cardUserGroup) {
        this.parent = cardUserGroup;
    }

    @Override
    public CardUserGroup getParent() {
        return parent;
    }

    @Override
    public void setChildren(List<CardUserGroup> children) {
        this.children = children;
    }

    @Override
    public List<CardUserGroup> getChildren() {
        return children;
    }

    @Override
    public int compare(CardUserGroup o1, CardUserGroup o2) {
        return Integer.parseInt(o1.getCode()) - Integer.parseInt(o2.getCode());
    }
}
